﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QL_Nha_Sach_Phuong_Nam.Quản_lý_sách.Quản_lý_tác_giả
{
    public partial class FormTacgia : Form
    {
        private Cxulytacgia xuly;
        private string ma;

        public FormTacgia()
        {
            InitializeComponent();
        }

        private void FormTacgia_Load(object sender, EventArgs e)
        {
            xuly = new Cxulytacgia();
            hienthidstacgia(xuly.getdsTacgia());
        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            FormEditTacGia f = new FormEditTacGia(0,"");
            f.ShowDialog();
            hienthidstacgia(xuly.getdsTacgia());
        }

        private void btnedit_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in lvtacgia.SelectedItems)
            {
                ma = lvi.SubItems[0].Text;
            }
            FormEditTacGia f = new FormEditTacGia(1,ma);
            f.ShowDialog();
            hienthidstacgia(xuly.getdsTacgia());
        }

        private void hienthidstacgia(List<Kết_nối.TacGia> ds)
        {
            lvtacgia.Items.Clear();
            foreach (Kết_nối.TacGia ncc in ds)
            {
                if (ncc.TacGiaStatus == "live")
                {
                    ListViewItem lvi = lvtacgia.Items.Add(ncc.TacGiaNo);
                    lvi.SubItems.Add(ncc.TacGiaName);
                    lvi.SubItems.Add(ncc.TacGiaSex);
                    lvi.SubItems.Add(ncc.TacGiaAddress);
                    lvi.SubItems.Add(ncc.TacGiaPhoneNumber);
                    
                }
            }
        }

        private void btnsearch_Click(object sender, EventArgs e)
        {
            if (txtten.Text == "")
            {
                hienthidstacgia(xuly.getdsTacgia());
            }
            else
            {
                List<Kết_nối.TacGia> ncc = xuly.TimTacgiatheoten(txtten.Text);
                if (ncc != null)
                {
                    hienthidstacgia(ncc);
                }
                else
                {
                    lvtacgia.Items.Clear();
                    MessageBox.Show("Không có tác giả này !!!");
                }
            }
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in lvtacgia.SelectedItems)
            {
                string ma = lvi.SubItems[0].Text;
                xuly.XoaTacgia(ma);
            }
            hienthidstacgia(xuly.getdsTacgia());
        }
    }
}
